package com.lancoo.ccas52.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lancoo.ccas52.entity.Student;
import com.lancoo.ccas52.entity.StudentRetake;
import com.lancoo.ccas52.mapper.StudentRetakeMapper;
import com.lancoo.ccas52.pojo.dto.StudentRetakeDto;
import com.lancoo.ccas52.pojo.response.basicPlatform.BaseStudent;
import com.lancoo.ccas52.pojo.response.score.Retake;
import com.lancoo.ccas52.service.CourseService;
import com.lancoo.ccas52.service.SegmentService;
import com.lancoo.ccas52.service.StudentRetakeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas52.util.BatchInsert;
import lombok.Data;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 重修学生表 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2023-04-10
 */
@Service
public class StudentRetakeServiceImpl extends ServiceImpl<StudentRetakeMapper, StudentRetake> implements StudentRetakeService {

    @Resource
    private StudentRetakeMapper studentRetakeMapper;

    @Resource
    private CourseService courseService;
    @Resource
    private SegmentService segmentService;

    @Override
    public void syncStudentRetake(Integer taskId, String schoolId, String term, List<Retake> retakes) {
        List<StudentRetake> studentRetakes = new ArrayList<>();
        for (Retake retake : retakes) {
            studentRetakes.add(retake.toEntity(taskId, schoolId, term));
        }
        //批量插入教学班学生，大数据批量插入
        Consumer<List<StudentRetake>> studentConsumer = o -> studentRetakeMapper.addBatch(o);
        BatchInsert.batchSplitInsert(studentRetakes, studentConsumer);
    }

//    @Override
//    @Cacheable(cacheNames = "RetakeStudent", key = "#teachingPlanId+'-'+#courseId+'-'+#flag")
//    public List<Student> getStudentRetake(Long teachingPlanId, Long courseId, Integer flag) {
//        String baseCourseId;
//        if (flag == 1) {
//            //获取课程基础平台id
//            baseCourseId = courseService.getById(courseId).getBaseCourseId();
//        } else {
//            //获取环节基础平台id
//            baseCourseId = segmentService.getById(courseId).getBaseSegmentId();
//        }
//        List<Student> studentByCourseIdFlag = studentRetakeMapper.getStudentByCourseIdFlag(baseCourseId, flag,null,null);
//        if (CollUtil.isNotEmpty(studentByCourseIdFlag)) {
//            //获取未分配教学计划的学生并分配
//            List<Long> retakeIds = studentByCourseIdFlag.stream().filter(o -> o.getTeachingPlanId() == null)
//                    .map(Student::getRetakeId).collect(Collectors.toList());
//            //非空就是已分配，空的就是未分配。返回列表要么为空要么就是所有
//            if (retakeIds.size() > 0) {
//                //分配教学计划
//                this.lambdaUpdate().in(StudentRetake::getRetakeId, retakeIds)
//                        .set(StudentRetake::getTeachingPlanId, teachingPlanId).update();
//                return studentByCourseIdFlag;
//            } else {
//                //判断是不是该教学计划的学生、
//                if (studentByCourseIdFlag.get(0).getTeachingPlanId().equals(teachingPlanId)) {
//                    return studentByCourseIdFlag;
//                } else {
//                    return new ArrayList<>();
//                }
//
//            }
//        }
//        return new ArrayList<>();
//    }

    @Override
    @Cacheable(cacheNames = "RetakeStudent", key = "#courseId+'-'+#flag+'-'+#studyNature+'-'+#campusId")
    public List<Student> getStudentRetakeByCourse(Long courseId, Integer flag, Integer studyNature, String campusId) {
        String baseCourseId;
        if (flag == 1) {
            //获取课程基础平台id
            baseCourseId = courseService.getById(courseId).getBaseCourseId();
        } else {
            //获取环节基础平台id
            baseCourseId = segmentService.getById(courseId).getBaseSegmentId();
        }
        List<Student> studentByCourseIdFlag = studentRetakeMapper.getStudentByCourseIdFlag(baseCourseId, flag, studyNature, campusId);
        return studentByCourseIdFlag;
    }

}
